package entity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import constans.Globals;
import constans.ResourceManager;
import controller.ObjectController;

import engine.Map;
import engine.MapMatrix;

public class Building extends Entity{

	protected int range;
	protected int attackSpeed;
	protected int damage;
	protected int imgX;
	protected int price;
	protected int splash;
	protected boolean bulletReady;
	protected boolean bulletRotate;
	
	protected Image bulletImg;
	protected Image bcg;
	
	protected Unit prey;
	protected float angle;
	
	protected String name;
	
	boolean canDrawRange = false;
	boolean canDraw = true;
	
	public Building(int x, int y)
	{
		super(x, y);	
		type = Entity.BUILDING;
	}
	
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		
		if(!canDraw) return;
		if(picked)
		{
			g.setColor(Color.green);
			g.fillRoundRect(Map.dx + x, y, size, size,5);
		}
		if(canDrawRange)
		{
			g.setColor(Color.black);
			int r = range * Globals.FIELD_SIZE;
			g.drawOval(Map.dx + x - r, y - r, 2*r + Globals.FIELD_SIZE, 2*r + Globals.FIELD_SIZE);
		}
		if(prey != null)
			setAngle();
		System.out.println(angle);
		bcg.draw(Map.dx + x, y, Map.dx + x + size, y + size, imgX, 0, imgX + size, size);
		img.setCenterOfRotation(15, 15);
		img.rotate((float) Math.toDegrees(angle));
		img.draw(Map.dx + x, y, Map.dx + x + size, y + size, imgX, 0, imgX + size, size);
		img.rotate((float) Math.toDegrees(-angle));
	}
	public void update(GameContainer container, int delta) throws SlickException
	{
		attack();
	}
	public void setRangeDrawing(boolean b)
	{
		canDrawRange = b;
	}
	public void build()
	{
		imgX = 60;
	}
	public void canBuild(boolean b)
	{
		if(b)
			imgX = 0;
		else
		{
			imgX = 30;
			setRangeDrawing(false);
		}
	}
	public void canDraw(boolean b)
	{
		canDraw = b;
	}
	private void attack()
	{
		if(!bulletReady) time();
		
		if(time % (800 / attackSpeed) == 0 && !bulletReady)
		{
			bulletReady = true;
			time = 0;
		}
		if(bulletReady)
		{
			this.prey = findPrey();
			if(prey != null)
			{	
				
				ObjectController.addBullet(new Bullet(getX(),getY(), prey, damage, range,splash, bulletImg, bulletRotate));
				bulletReady = false;
			}
		}
		
		
	}
	private Unit findPrey()
	{
		MapMatrix map = ObjectController.getMap();
		
		int r = 1;
		while(r <= range)
		{
			for(int i = -r; i < r ; i++)      
            {	       
				int i1 = this.i - i;
				int j1 = this.j - r;
				if(isHerePrey(i1,j1,map))
					return (Unit) map.getEntity(i1, j1);
				i1 = this.i - i;
				j1 = this.j + r;
				if(isHerePrey(i1,j1,map))
					return (Unit) map.getEntity(i1, j1);
				i1 = this.i - r;
				j1 = this.j - i;
				if(isHerePrey(i1,j1,map))
					return (Unit) map.getEntity(i1, j1);
				i1 = this.i + r;
				j1 = this.j - i;
				if(isHerePrey(i1,j1,map))
					return (Unit) map.getEntity(i1, j1);
            }          	
			r++;
		}
		return null;
	}
	private boolean isHerePrey(int i1, int j1, MapMatrix map)
	{		
		boolean t = false;
		if(i1 >= 0 && i1 < Globals.MAP_I && j1 >= 0 && j1 < Globals.MAP_J)
		{
			if(range == 1)
				t = true;
			if(!t)	
				//dodaje 0.5, zeby zaokraglic zasieg
				if ((float)range + 0.5f >= Math.sqrt( Math.pow(i - i1,2) + Math.pow(j - j1,2)))
					t = true;
		}
		if(t)		
			if(!map.isFieldEmpty(i1, j1))
				if(map.getEntity(i1, j1).getType() == Entity.UNIT)
					return true;
		return false;
	}
	public int getPrice()
	{
		return price;
	}
	public int getRange() {
		return range;
	}
	public int getSplash() {
		return splash;
	}

	public int getAttackSpeed() {
		return attackSpeed;
	}

	public int getDamage() {
		return damage;
	}

	public String getName() {
		return name;
	}

	public boolean isOn(int mx, int my) {
		if(mx > x && mx < x+size)
			if(my > y && my < y+size)
				{
					canDrawRange = true;
					picked = true;
					return true;
				}
		canDrawRange = false;
		return false;
	}
	private void setAngle()
	{
		//if((int)prey.y == (int)y) return;
		if((int)prey.y > (int)y)
			angle = (float) (Math.toRadians(-90) - Math.acos((x - prey.getX()) / Math.sqrt(Math.pow(y - prey.getY(), 2) + Math.pow(x - prey.getX(),2))));
		else
			angle = (float) (Math.toRadians(-90) + Math.acos((x - prey.getX()) / Math.sqrt(Math.pow(y - prey.getY(), 2) + Math.pow(x - prey.getX(),2))));
	}
	

}
